﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySearch;


public partial class User_ProfileMaintenance : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    { 
        BindData();
    }

    public void BindData()
    {
        string iid = Utilities.getLoginIID();

        // prepare client chargeable
        //HashSet<KeyValuePair<Int64, Int64>> dictProfileChargeable = null;

        //IList<Profile> lstProfile = null;//DataUtil.ListProfile(iid, out dictProfileChargeable);
        IList<Profile> lstProfile = ProfileOM.ListProfile(iid);

        CreateRepeater(this.rptProfile, this.hdSortBy.Value, lstProfile);
        this.rptProfile.ItemCommand += new RepeaterCommandEventHandler(List_ItemCommand);
    }

    protected void CreateRepeater(Repeater rptList, string strSortby, IList<Profile> source)
    {
        #region Define column definition
        IList<KpmgRepeaterColumnDef> lstCols = new List<KpmgRepeaterColumnDef>();
        IDictionary<string, string> dic = new Dictionary<string, string>();

        dic.Add("AscImage", this.ResolveUrl("~/images/sort_asc.gif"));
        dic.Add("DescImage", this.ResolveUrl("~/images/sort_desc.gif"));

        lstCols.Add(KpmgRepeaterColumnDef.CreateColumnData("Opportunity code", "oppCode", 20, true));
        lstCols.Add(KpmgRepeaterColumnDef.CreateColumnData("Opportunity desc", "oppDesc", 25, true));
        lstCols.Add(KpmgRepeaterColumnDef.CreateColumnData("Client", "client", 30, true));
        lstCols.Add(KpmgRepeaterColumnDef.CreateColumnData("Actions", "actions", 25, false));

        #endregion

        #region Create repeater
        string[] arrSortBy = strSortby.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        IList<IDictionary<string, KpmgRepeaterColumnData>> lst = CreateData(source);
        SortType type = arrSortBy.Length == 2 ? SortType.Str : (SortType)Enum.Parse(typeof(SortType), arrSortBy[2]);
        KpmgRepeater rpt =
            new KpmgRepeater(lstCols, lst, arrSortBy[0],
                arrSortBy[1].ToUpper() == MySearch.SortDirection.Asc.ToString().ToUpper(),
                dic,
                type);

        rptList.HeaderTemplate = rpt.GetTemplate(ListItemType.Header);
        rptList.ItemTemplate = rpt.GetTemplate(ListItemType.Item);
        rptList.FooterTemplate = rpt.GetTemplate(ListItemType.Footer);

        IList<int> lstData = new List<int>();
        for (int i = 0; i < lst.Count; i++)
            lstData.Add(0);

        rptList.DataSource = lstData;
        rptList.DataBind();

        #endregion
    }

    protected IList<IDictionary<string, KpmgRepeaterColumnData>> CreateData(IList<Profile> source)
    {
        IList<IDictionary<string, KpmgRepeaterColumnData>> lstRet = new List<IDictionary<string, KpmgRepeaterColumnData>>();

        foreach (var profile in source)
        {
            IDictionary<string, KpmgRepeaterColumnData> dicDataRow = new Dictionary<string, KpmgRepeaterColumnData>();

            dicDataRow.Add("oppCode", new KpmgRepeaterColumnData(profile.ProfileCode, profile.ProfileCode));
            var oppDesc = profile.Description;
            dicDataRow.Add("oppDesc", new KpmgRepeaterColumnData(oppDesc, oppDesc));

            var client = profile.Fields["Client_Code"].Value;
            client += " - " + profile.Fields["Client_Name"].Value;
            dicDataRow.Add("client", new KpmgRepeaterColumnData(client, client));

            string actionUrl = "RemotePost.aspx?RequestUrl=EditProfile.aspx&OBJECT_ID=" + profile.ProfileCode;
            string actionCommand = "Edit item and create ticket";
            string strAction = @"&nbsp;<a href='" + actionUrl + "' class='FormButton2013'><span class=''>" + actionCommand + "</span></a>";
            dicDataRow.Add("actions", new KpmgRepeaterColumnData(strAction, ""));
            lstRet.Add(dicDataRow);
        }
        return lstRet;
    }

    protected void List_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        KpmgRepeaterAction act;
        if (!Enum.TryParse<KpmgRepeaterAction>(e.CommandName, true, out act))
            return;

        #region Excute command
        switch (act)
        {
            case KpmgRepeaterAction.Sort:
                this.hdSortBy.Value = e.CommandArgument.ToString();
                BindData();
                break;
            default:
                throw new Exception("Action not implemented");

        }
        #endregion
    }
  

 
}